<template>
	<view class="index">
		<!-- v-bind:markers="markers" -->
		<map id="myMap" v-bind:latitude="location.latitude" v-bind:longitude="location.longitude"
			:show-location="true"></map>
		<view class="list" v-if="orders.length!=0" v-for="(item,index) in orders" :key="index">
			<view class="item">
				<view class="text">
					<view class="username">
						订单ID：{{item.orderId}}
					</view>
					<view class="">
						乘客上车点：{{item}}
					</view>
				</view>
			</view>
		</view>

		<view class="get-order">
			<view class="button">
				<button type="primary" style="background-color: #58f;" @click="getNearbyOrder">获取附近订单</button>
			</view>

			<view class="info">
				<view class="where">
					当前位置：
					<view class="address">
						<text>{{location.address}}</text>
						<text>{{location.name}}</text>
					</view>

				</view>
				<view class="profile" @click="toDriverInfo">
					司机信息
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	import amapFile from '@/libs/amap-wx.js';
	import utils from "../../common/utils.js";
	const api = utils.websiteUrl;
	export default {
		data() {
			return {
				myAmapFun: undefined,
				location: {
					name: '',
					address: '',
					longitude: 30.0011111,
					latitude: 89.1111111
				},
				//定义经纬度
				// longitude: '',
				// latitude: '',
				markers: [],
				//存放乘客目的地
				target: {},
				// 存放乘客起点
				start: {},
				token: '',
				// 选择的订单信息
				order: {},
				// 所有订单
				orders: [],
				// 乘客用户名
				userName: '',
				// 用户起始和目的地
				passenger: {},
				// 乘客是否上车标记
				isGetInCar: false,
				// 乘客Id
				passagerId: undefined,
				// 结束订单信息，暂存
				orderOverInfo: {},
				// 暂存逆地址编码信息
				describe: {}


			}
		},
		onLoad() {
			//2.创建实例对象，以便于后面调用它的方式，实现相关功能
			//传入自己的key
			this.myAmapFun = new amapFile.AMapWX({
				key: '85208405250c458368d44bccd8104998'
			});

			//3.调用方法获取位置信息(可以封装成一个函数)
			//在加载时调用位置信息
			this.getLocation();
			// this.forGetNearbyOrder();
			this.token = uni.getStorageSync('token')


		},
		onShow() {

		},
		methods: {
			// 前往司机个人信息
			toDriverInfo() {
				let that = this;
				uni.redirectTo({
					url: "/pages/driver_info/driver_info",
					success: () => {
						console.log('success to driver profile')
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			getLocation() {
				const that = this;
				//调用方法获取位置信息
				this.myAmapFun.getRegeo({
					//成功的回调函数(获取位置信息成功)
					success: (res) => {
						console.log('当前位置信息', res);
						this.location.name = res[0].desc;
						this.location.longitude = res[0].longitude;
						this.location.latitude = res[0].latitude;
						console.log(res[0].desc);
						console.log(this.location)

						//添加标记点
						// let obj = {
						// 	id: 1,
						// 	latitude: this.location.latitude,
						// 	longitude: this.location.longitude,
						// 	iconPath: "../../static/司机当前位置.png",
						// 	width: 40,
						// 	height: 40
						// }
						// this.markers = []
						// this.markers.push(obj)
					},
					//失败的回调函数(获取位置信息失败)
					fail: (err) => {
						console.log('获取位置失败0', err);
					}
				})
			},
			// 获取附近打车订单
			getNearbyOrder() {
				let that = this;
				uni.request({
					url: api + "order/getNearbyOrder",
					method: "GET",
					sslVerify: false,
					data: {
						positionLatitude: that.location.latitude,
						positionLongitude: that.location.longitude
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						"token": that.token
					},
					success: (res) => {
						console.log('获取附近订单')
						console.log(res)
						if (res.data.code == '0') {
							console.log(res.data.data)
							that.orders = res.data.data;

							// 选择订单才加入
							// that.markers = []
							// let obj1 = {
							// 	id: 0,
							// 	latitude: that.passenger.position.positionLatitude,
							// 	longitude: that.passenger.position.positionLongitude,
							// 	iconPath: "../../static/起点.png",
							// 	width: 40,
							// 	height: 40
							// }
							// that.markers.push(obj1)
							// let obj2 = {
							// 	id: 1,
							// 	latitude: that.passenger.positionEnd.positionLatitude,
							// 	longitude: that.passenger.positionEnd.positionLatitude,
							// 	iconPath: "../../static/终点.png",
							// 	width: 40,
							// 	height: 40
							// }
							// that.markers.push(obj2)
							// uni.redirectTo({
							// 	url: '/pages/index_driver/index_driver'
							// })
							that.useDescribe(that.orders)


						} else {
							uni.showToast({
								duration: 1000,
								title: "附近没有订单",
								icon: 'error'
							})
						}
					},
					fail: (err) => {
						console.log(err)
					}
				})
			},
			// 间隔时间获取订单
			forGetNearbyOrder() {
				let that = this;
				setInterval(function() {
					that.getNearbyOrder();
				}, 1000)
			},
			// 获取司机的进行中订单(司机)
			getDriverOrder() {
				let that = this;
				uni.request({
					url: api + 'order/getDriverOrder',
					method: "GET",
					sslVerify: false,
					data: {

					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						"token": that.token
					},
					success: (res) => {
						console.log(res)
						if (res.data.code == '0') {
							that.start.latitude = res.data.data.positionStartLatitude
							that.start.longitude = res.data.data.positionStartLongitude

							that.target.latitude = res.data.data.positionEndLatitude
							that.target.latitude = res.data.data.positionEndLongitude

							that.order.driverId = res.data.data.driverId
							that.order.passengerId = res.data.data.passengerId
							that.order.beginTime = res.data.data.beginTime
							that.order.id = res.data.data.id
							that.order.state = res.data.data.state
						}
						if (res.data.code == '1') {
							uni.showToast({
								duration: 1000,
								title: "没有进行中订单",
								position: 'center',
								icon: 'error'
							})
						}
					},
					fail: (err) => {
						console.log(err)

					}
				})
			},
			// 乘客上车
			GetOnTheCar() {
				let that = this;
				uni.request({
					url: api + 'order/GetOnTheCar',
					sslVerify: false,
					method: 'GET',
					data: {
						'userName': that.userName
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						"token": that.token
					},
					success: (res) => {
						console.log(res)
						if (res.data.code == '0') {
							console.log('上车成功')
							that.isGetInCar = true
						}
						if (res.data.code == '1') {
							console.log('上车失败')
							that.isGetInCar = false
						}
					},
					fail: (err) => {
						console.log('上车请求失败')
						console.log(err)
					}
				})
			},
			// 接单
			ReceivingOrders() {
				let that = this;
				uni.request({
					url: api + 'order/ReceivingOrders',
					sslVerify: false,
					method: 'GET',
					data: {
						passagerId: that.passagerId
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						"token": that.token
					},
					success: (res) => {
						console.log(res)
						if (res.data.code == '0') {
							console.log('接单成功')
						}
					},
					fail: (err) => {
						console.log('接单失败')
					}
				})
			},
			// 订单结束
			orderOver() {
				let that = this;
				uni.request({
					url: api + 'order/orderOver',
					method: 'GET',
					sslVerify: false,
					data: {
						positionLatitude: that.location.latitude,
						positionLongitude: that.location.longitude
					},
					header: {
						'content-type': 'application/x-www-form-urlencoded;charset=utf-8',
						"token": that.token
					},
					success: (res) => {
						console.log('订单成功结束')
						if (res.data.code == '0') {
							that.orderOverInfo = res.data.data
						}
					},
					fail: (err) => {
						console.log('订单结束失败')
						console.log(err)
					}
				})
			},
			// 调用逆地址编码获取信息
			async useDescribe(orders) {
				let that = this;
				for (var i = 0; i < orders.length; i++) {
					await that.getDescribe(orders[i]);
					console.log("调用逆地址编码获取信息")
					console.log(that.describe.formatted_address)
					that.orders[i]['describe'] = that.describe.formatted_address
				}
			},
			// 逆地址编码
			getDescribe(item) {
				let that = this;
				uni.request({

					url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
					methods: 'GET',
					sslVerify: false,
					data: {
						key: '87ae0042dce7c543b9be96be129ea404',
						location: String(Number(item.positionStart.positionLatitude).toFixed(6)) + ',' + String(
							Number(item.positionStart.positionLongitude).toFixed(6)),
						extensions: 'all'
					},
					success: (res) => {
						console.log("逆地址编码")
						console.log(res)
						if (res.data.status == 1) {
							that.describe = res.data.regeocode
							console.log(that.describe.formatted_address)
						}
					},
					fail: (err) => {
						console.log("逆地址编码失败")
						console.log(err)
					}

				})
			}



		}
	}
</script>

<style lang="scss">
	.index {
		width: 100vw;
		height: 100vh;

		//地图
		#myMap {
			width: 100vw;
			height: 100vh;
		}

		.list {
			width: 98vw;
			background-color: #fff;
			//固定定位
			position: fixed;
			left: 1vw;
			bottom: 90vh;
			max-height: 50vh;
			box-shadow: 1px 1px 4px 4px #ccc;
			// .list {
			// 	height: calc(100%-120rpx);
			overflow: scroll;
			// }
		}

		.item {
			height: 120rpx;
			border-bottom: 1px solid #eee;
			display: flex;
			align-items: center;
		}

		.get-order {
			width: 98vw;
			background-color: #fff;
			//固定定位
			position: fixed;
			left: 1vw;
			bottom: 5vw;
			box-shadow: 1px 1px 4px 4px #ccc;


			.info {
				.where {
					width: 50%;
					height: 100rpx;
					// line-height: 100rpx;
					background-color: #fff;
					border-right: solid #eee;
					line-height: 50rpx;

					.address {

						font-size: 15px;
					}
				}

				.profile {
					height: 100rpx;
					border: 1px solid #eee;
					// display: flex;
					// padding: 0 40rpx;
					align-items: center;
					text-align: center;
					line-height: 100rpx;
					width: 50%;
				}

				display: flex;
				align-content: space-around;


			}
		}

	}
</style>